#include <iostream>
#include <fstream>
#include "chontachs.pb.h"

void PrintContact(chontachs1::Contacts &contact)
{
	for (int i = 0; i < contact.contacts_size(); i++)
	{
		chontachs1::PeopleInfo people = contact.contacts(i);
		std::cout << "-----------联系人(" << i + 1 << ")-----------" << std::endl;
		std::cout << "姓名：" << people.name() << std::endl;
		std::cout << "年龄：" << people.age() << std::endl;
		int j = 1;
		for (const auto phone : people.phone())
		{
			std::cout << "电话(" << j++ << "): " << phone.number() << "(" << phone.PhoneType_Name(phone.type())
					  << ")" << std::endl;
		}
		if (people.has_data() && people.data().Is<chontachs1::Address>())
		{
			chontachs1::Address address;
			people.data().UnpackTo(&address);
			if (!address.home_address().empty())
			{
				std::cout << "家庭地址：" << address.home_address() << std::endl;
			}
			if (!address.unit_address().empty())
			{
				std::cout << "单位地址：" << address.unit_address() << std::endl;
			}
		}

		switch (people.other_contact_case())
		{
		case chontachs1::PeopleInfo::OtherContactCase::kQq :
			std::cout << "qq号: " << people.qq() << std::endl;
			break;
		case chontachs1::PeopleInfo::OtherContactCase::kWechat :
			std::cout << "微信号: " << people.wechat() << std::endl;
			break;
		default:
			break;
		}

		if(people.remark_size())
		{
			std::cout << "备注信息：" << std::endl;
		}
		for(auto it = people.remark().cbegin();it != people.remark().cend();it++)
		{
			std::cout << "  " << it->first << ": " << it->second << std::endl;
		}
	}
}

int main()
{
	chontachs1::Contacts contact;
	// 从本地文件中读取联系人
	std::fstream input("contach.bin", std::ios::in | std::ios::binary);
	if (!input.is_open())
	{
		std::cout << "open error" << std::endl;
	}
	else
	{
		// 打开文件成功进行反序列化
		contact.ParseFromIstream(&input);
	}

	// 打印联系人列表
	PrintContact(contact);
	input.close();
	// std::cout << "succeed---" << std::endl;
	return 0;
}